package com.jang.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;


@Component
@WebFilter(urlPatterns = { "/*" }, filterName = "myFilter")
public class myFilter implements Filter {
    @Override
    public void destroy() {

        System.out.println("服务关闭，调用过滤器Filter的销毁方法destroy()..........");
    }
    @Override
    public void init(FilterConfig var1)throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("测试过滤器");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rep = (HttpServletResponse) response;
        /*BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(),UTF8));
            char[] chars = new char[111111111];
            int s =IOUtils.read(reader,chars);
            String body = new String(chars).substring(0,s);
            Object data = JSON.parseObject(body);
            System.out.println("::"+data);*/
/*
        java.lang.reflect.Field filter = data.getClass().getDeclaredField("name");
*/
        if (false) {// 验证失败
            PrintWriter writer = null;
            OutputStreamWriter osw = null;
            try {
                osw = new OutputStreamWriter(response.getOutputStream(),
                        "UTF-8");
                writer = new PrintWriter(osw, true);
                String jsonStr = JSON.toJSONString("sss");
                writer.write(jsonStr);
                writer.flush();
                writer.close();
                osw.close();
            } catch (IOException e) {
            }

        } else {
            filterChain.doFilter(request, response);

        }
    }

}
